//
//  RadioViewController.m
//  口袋
//
//  Created by why on 15/5/29.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "RadioViewController.h"
#import "RadioHeadView.h"
#import "RadioCell.h"
#import "SANetWorkingTask.h"
#import <AFNetworking.h>
#import "RadioListModel.h"
#import "PlayViewController.h"
#import <MJRefresh.h>
#import "DKNightVersion.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface RadioViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *titleArray;
@end

@implementation RadioViewController
- (void)dealloc
{
    [_table release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
       // self.arr = [[NSMutableArray array];
        self.titleArray = [NSMutableArray array];
        self.page = 2;
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
    self.navigationController.navigationBar.translucent = NO;
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
   // self.navigationItem.title = @"口袋电台";
   // [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:128/255.0 green:217/255.0 blue:215/255.0 alpha:1.0]];
    
    [SANetWorkingTask request:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/278614/true/1/15" parmater:nil block:^(NSDictionary *result) {
        NSDictionary *bigDic = [result objectForKey:@"tracks"];
        NSArray *array = [bigDic objectForKey:@"list"];
        self.arr = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            RadioListModel *model = [[RadioListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
            [self.arr addObject:model.playUrl32];
            [self.titleArray addObject:model.title];
        }
        [self createTable];
    }];
    
    

}



- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
    self.table.rowHeight = 0.35 * WIDTH;
    [self.table setSeparatorColor:[UIColor colorWithRed:118/255.0 green:210/255.0 blue:245/255.0 alpha:1.0]];
    RadioHeadView *head = [[RadioHeadView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0.45 * WIDTH)];
     [head setBackgroundColor:[UIColor colorWithRed:118/255.0 green:210/255.0 blue:245/255.0 alpha:1.0]];
    head.nightBackgroundColor = UIColorFromRGB(0x343434);
    self.table.tableHeaderView = head;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    [self.table addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(load)];
    [self.view addSubview:self.table];
    [self.table release];
}

- (void)refresh
{
    [self.table reloadData];
    [self.table.header endRefreshing];
}

- (void)load
{
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/278614/true/%ld/15", (long)self.page];
    [SANetWorkingTask request:url parmater:nil block:^(NSDictionary *result) {
        NSDictionary *bigDic = [result objectForKey:@"tracks"];
        NSArray *array = [bigDic objectForKey:@"list"];
//        for (NSDictionary *dic in array) {
//            RadioListModel *model = [[RadioListModel alloc]init];
//            [model setValuesForKeysWithDictionary:dic];
//            [self.array addObject:model];
//        }
        if (array.count == 0) {
            [self.table removeFooter];
            return ;
        }
        for (NSDictionary *dic in array) {
            RadioListModel *model = [[RadioListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        self.page += 1;
        [self.table.footer endRefreshing];
        [self.table reloadData];

    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[RadioCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
    if (self.array.count > indexPath.row) {
        RadioListModel *model = [self.array objectAtIndex:indexPath.row];
        cell.titleLabel.text = model.title;
        //给label设置高度;
        cell.titleLabel.font = [UIFont boldSystemFontOfSize:19];
        NSDictionary *dicc = @{NSFontAttributeName:[UIFont systemFontOfSize:19]};
        CGRect rect = [model.title boundingRectWithSize:CGSizeMake(250, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dicc context:nil];
        [cell.titleLabel setFrame:CGRectMake(cell.headView.frame.origin.x + cell.headView.frame.size.width + 0.03 * WIDTH, 0.05 * WIDTH, 0.65 * WIDTH, HEIGHT / 12)];
        cell.titleLabel.numberOfLines = 0;

        NSString *source = [NSString stringWithFormat:@"by  %@", model.nickname];
        cell.sourceLabel.text = source;
        [cell.sourceLabel setFrame:CGRectMake(cell.titleLabel.frame.origin.x, cell.headView.frame.origin.y + cell.headView.frame.size.height - 0.06 * WIDTH, 0.5 * WIDTH, 0.05 * WIDTH)];
        NSString *num = [NSString stringWithFormat:@"%@", model.playtimes];
        cell.hearLabel.text = num;
        cell.hearLabel.backgroundColor = [UIColor clearColor];
        [cell.hearLabel setFrame:CGRectMake(cell.titleLabel.frame.origin.y + cell.titleLabel.frame.size.width + 0.06 * WIDTH, cell.sourceLabel.frame.origin.y, 0.3 * WIDTH, 0.05 * WIDTH)];
        
        [cell.hear setFrame:CGRectMake(WIDTH - cell.hearLabel.frame.size.width, cell.hearLabel.frame.origin.y, 0.04 * WIDTH, 0.04 * WIDTH)];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        RadioListModel *model = [self.array objectAtIndex:indexPath.row];
    NSString *str = model.nickname;
    NSDictionary *dicc = @{NSFontAttributeName:[UIFont systemFontOfSize:19]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(250, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dicc context:nil];
    return rect.size.height + WIDTH * 0.294;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioListModel *model = [self.array objectAtIndex:indexPath.row];
    PlayViewController *play = [[PlayViewController alloc]init];
    play.array = self.arr;
    play.url = model.playUrl32;
    play.titleArray = self.titleArray;
    play.labelTitle = model.title;
    [play setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:play animated:YES completion:nil];
    [play release];
    
}

@end
